import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "../store";

Vue.use(VueRouter)

// // 解决报错
// const originalPush = Router.prototype.push
// const originalReplace = Router.prototype.replace
// // 针对 push 方法
// store.prototype.push = function push (location, onResolve, onReject) {
//   if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
//   return originalPush.call(this, location).catch(err => err)
// }

const routes = [
  {
    path: '/login',
    name: '登录',
    component: () =>import('../views/Login.vue')
  },
  {
    path: '/register',
    name: '注册',
    component: () =>import('../views/Register.vue')
  },
  {
    path: '/retrieve',
    name: '找回密码',
    component: () =>import('../views/Retrieve.vue')
  },
  {
    path: '/front',
    name: 'Front',
    component: () => import('../views/front/Front'),
    children: [
      {
        path: 'home',
        name: 'FrontHome',
        component: () => import('../views/front/Home.vue')
      },
      {
        path: 'person',
        name: 'FrontPerson',
        component: () => import('../views/front/Person.vue')
      },
      {
        path: 'password',
        name: 'FrontPassword',
        component: () => import('../views/front/Password.vue')
      },
      {
        path: 'cart',
        name: 'FrontCart',
        component: () => import('../views/front/Cart.vue')
      },
      {
        path: 'order',
        name: 'FrontOrder',
        component: () => import('../views/front/Order.vue')
      },
      {
        path: 'notice',
        name: 'FrontNotice',
        component: () => import('../views/front/Notice.vue')
      },
      {
        path: 'noticeDetail',
        name: 'NoticeDetail',
        component: () => import('../views/front/NoticeDetail.vue')
      },{
        path: 'goods',
        name: 'Goods',
        component: () => import('../views/front/Goods.vue')
      },{
        path: 'goodsDetail',
        name: 'GoodsDetail',
        component: () => import('../views/front/GoodsDetail.vue')
      },
    ]
  },
  {
    path: '/404',
    name: '404',
    component: () => import('../views/404.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//设置动态路由 刷新页面会重置路由
export const setRotes = () =>{
  const storeMenus = localStorage.getItem("menu");
  if (storeMenus){
    //拼装动态路由
    const manageRoute = {path:'/',component:() => import('../views/Manage.vue'),redirect: "/home",children: [
            { path: 'person', name: '个人信息', component: () => import('../views/Person.vue')},
            { path: 'password', name: '修改密码', component: () => import('../views/Password.vue')},
            { path: 'NoticeDetail', name: '公告详情', component: () => import('../views/NoticeDetail.vue')},
            { path: 'ReportDetail', name: '打卡详情', component: () => import('../views/ReportDetail.vue')}
        ]}
    const menu = JSON.parse(storeMenus)
    menu.forEach(item =>{
      if (item.path){ //当且仅当path不为空的时候才去设置路由
        let itemMenu = { path: item.path.replace("/",""), name: item.name,component:() => import('../views/'+item.pagePath + '.vue')}
        manageRoute.children.push(itemMenu)
      }else if(item.children.length){
        item.children.forEach(item =>{
          if (item.path){
            let itemMenu = { path: item.path.replace("/",""), name: item.name,component:() => import('../views/'+item.pagePath + '.vue')}
            manageRoute.children.push(itemMenu)
          }
        })
      }
    })
    //动态添加到现在的路由对象当中 获取当前名称数组 不包含Manage对象名称在添加
    const curRouts = router.getRoutes().map(v => v.name)
    if (!curRouts.includes('Manage')){
      router.addRoute(manageRoute)
    }
  }
}
//重置
setRotes()

//路由守卫
router.beforeEach((to, from, next) => {
  localStorage.setItem("currentPathName", to.name)  // 设置当前的路由名称
  store.commit("setPath") //触发store数据更新
  // 未找到路由的情况
  if (!to.matched.length) {
    const storeMenus = localStorage.getItem("menu")
    if (storeMenus) {
      next("/404")
    } else {
      // 跳回登录页面
      next("/login")
    }
  }
  // 其他的情况都放行
  next() //放行路由
})

export default router
